package com.itouchtv.yang.core.driver;

import com.itouchtv.yang.core.eventListener.MySeleniumEventListener;
import com.itouchtv.yang.core.tools.ClassPath;
import com.itouchtv.yang.core.tools.ConfigManager;
import com.itouchtv.yang.core.tools.Log;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.LocalFileDetector;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import org.testng.Assert;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * 根据配置文件，选择使用不同浏览器
 */
class DriverSelect {

    private DriverSelect(){
    }

    static EventFiringWebDriver selectRemoteDriver(String... driverName){
        String driver;
        if (driverName.length==0){
            driver = ConfigManager.getProperty("Browser", "");
            driver = driver.toUpperCase();
        }else driver = driverName[0].toUpperCase();

        switch (driver) {
            case "CHROME": {
                Log.logger.info("使用远程浏览器:" + driver);
                DesiredCapabilities capability = new DesiredCapabilities();
                capability.setBrowserName("chrome");
                capability.setCapability("name", "Remote File Upload using Selenium 3's FileDetectors");
                capability.setCapability("arguments", "disable-infobars");
                RemoteWebDriver remoteWebDriver;
                try {
                    remoteWebDriver = new RemoteWebDriver(new URL(ConfigManager.getProperty("seleniumServer", ""))
                            , capability);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                }
                remoteWebDriver.setFileDetector(new LocalFileDetector());
                return new EventFiringWebDriver(remoteWebDriver).register(new MySeleniumEventListener());

            }
            case "FIREFOX": {

                Log.logger.info("使用远程浏览器:" + driver);
                DesiredCapabilities capability = new DesiredCapabilities();
                capability.setBrowserName("firefox");
                capability.setCapability("name", "Remote File Upload using Selenium 3's FileDetectors");

                RemoteWebDriver remoteWebDriver;
                try {
                    remoteWebDriver = new RemoteWebDriver(new URL(ConfigManager.getProperty("seleniumServer", ""))
                            , capability);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                }
                remoteWebDriver.setFileDetector(new LocalFileDetector());
                return new EventFiringWebDriver(remoteWebDriver).register(new MySeleniumEventListener());

            }
            case "IE": {

                Log.logger.info("使用远程浏览器:" + driver);
                DesiredCapabilities capability = DesiredCapabilities.internetExplorer();
                capability.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION, true);
                capability.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                capability.setCapability("name", "Remote File Upload using Selenium 3's FileDetectors");

                RemoteWebDriver remoteWebDriver;
                try {
                    remoteWebDriver = new RemoteWebDriver(new URL(ConfigManager.getProperty("seleniumServer", ""))
                            , capability);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    return null;
                }
                remoteWebDriver.setFileDetector(new LocalFileDetector());
                return new EventFiringWebDriver(remoteWebDriver).register(new MySeleniumEventListener());
            }
            default:
                Log.logger.error("浏览器配置错误，请检查config.properties");
                Assert.fail("浏览器配置错误，请检查config.properties");
                return null;
        }
    }

    static EventFiringWebDriver selectDriver(String... driverName){
        String path = ClassPath.getClassPath();
        String driver;
        String os = System.getProperty("os.name");
        if (driverName.length==0){
            driver = ConfigManager.getProperty("Browser", "");
            driver = driver.toUpperCase();
        }else driver = driverName[0].toUpperCase();

        switch (driver){
            case "CHROME" :
                Log.logger.info("使用浏览器:" + driver);
                System.setProperty("webdriver.chrome.driver", path+"chromedriver.exe");
                ChromeOptions options = new ChromeOptions();
                options.addArguments("disable-infobars");
                return new EventFiringWebDriver(new ChromeDriver(options)).register(new MySeleniumEventListener());

            case "FIREFOX":
                Log.logger.info("当前系统:" + os);
                Log.logger.info("使用浏览器:" + driver);

                FirefoxOptions firefoxOptions = new FirefoxOptions();
                if (os.toUpperCase().contains("LINUX")){
                    Log.logger.info("this is linux");
                    firefoxOptions.addArguments("-headless");
                    System.setProperty("webdriver.gecko.driver","/home/geckodriver");
                }else{
                    System.setProperty("webdriver.gecko.driver", path+"geckodriver.exe");
                    String pathFire = ConfigManager.getProperty("FireFoxPath","");
                    System.setProperty("webdriver.firefox.bin", pathFire);
                }
                return new EventFiringWebDriver(new FirefoxDriver(firefoxOptions)).register(new MySeleniumEventListener());

            case "IE" :
                Log.logger.info("使用浏览器:" + driver);
                System.setProperty("webdriver.ie.driver", path+"IEDriverServer.exe");
                InternetExplorerOptions option = new InternetExplorerOptions();
                option.setCapability(InternetExplorerDriver.IGNORE_ZOOM_SETTING,true);
                option.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
                option.setCapability(InternetExplorerDriver.INITIAL_BROWSER_URL, "http://www.bing.com");
                option.setCapability(InternetExplorerDriver.IE_ENSURE_CLEAN_SESSION,true);
//                options.setCapability(InternetExplorerDriver.LOG_LEVEL,"WARN");
                return new EventFiringWebDriver(new InternetExplorerDriver(option)).register(new MySeleniumEventListener());

            case "ANDROID":
                Log.logger.info("使用设备:" + driver);
                DesiredCapabilities capabilities = new DesiredCapabilities();
//                capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
                capabilities.setCapability("platformName", "Android");
                capabilities.setCapability("deviceName","Android Emulator");
//                capabilities.setCapability("platformVersion", "4.2.2");
//                capabilities.setCapability("app", app.getAbsolutePath());
                capabilities.setCapability("noReset",true);
                capabilities.setCapability("appPackage", "com.touchtv.touchtv");
                capabilities.setCapability("appActivity", "com.gdtv.module.main.view.activity.ChangeIPActivity");
                URL url = null;
                try {
                    url = new URL("http://127.0.0.1:4723/wd/hub");
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
                AndroidDriver androidDriver = new AndroidDriver(url,capabilities);
                androidDriver.setFileDetector(new LocalFileDetector());
                return new EventFiringWebDriver(androidDriver).register(new MySeleniumEventListener());

            default :
                Log.logger.error("浏览器配置错误，请检查config.properties");
                Assert.fail("浏览器配置错误，请检查config.properties");
                return null;
        }
    }

}
